package com.service.impl;

import com.common.exception.db.DeleteException;
import com.common.exception.db.InsertException;
import com.common.exception.db.UpdateException;
import com.common.paging.PagingData;
import com.dao.LocationDao;
import com.dao.SeekTeacherDao;
import com.dto.in.ResumeInfo;
import com.entity.*;
import com.service.SeekTeacherService;


import net.sf.json.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SeekTeacherServiceimpl implements SeekTeacherService {

    @Autowired
    private SeekTeacherDao seekTeacherDao;

    @Autowired
    private LocationDao dao;

    @Override
    public List<ListTeacherInfo> seekTeacher(String teacherName,String teacherSex,String cityName,Integer teacherValue, Integer orderCount, String teachingStage, String schoolId, String subjectId, String teachingAreaId, Integer limit) {
        String asdd = "";
        for (int a = 0; a < cityName.length(); a++) {
            asdd = String.valueOf(cityName.charAt(a));
            if (asdd.equals("市")) {
                cityName = cityName.replace("市", "");
            }
        }
        JSONArray SchoolIdS = JSONArray.fromObject(schoolId);
        JSONArray TeachingStageS = JSONArray.fromObject(teachingStage);
        JSONArray TeachingAreaIdS = JSONArray.fromObject(teachingAreaId);
        JSONArray SubjectIdS = JSONArray.fromObject(subjectId);
        Integer cityId = dao.findCityIsBeing(cityName);
        if (cityId != null) {
            if(SubjectIdS.size() == 0){
                List<ListTeacherInfo> list=seekTeacherDao.seekTeacher1(teacherName,teacherSex,cityId,teacherValue,orderCount,TeachingStageS,SchoolIdS,SubjectIdS,TeachingAreaIdS,limit);
                return list;
            }else{
                List<ListTeacherInfo> list=seekTeacherDao.seekTeacher2(teacherName,teacherSex,cityId,teacherValue,orderCount,TeachingStageS,SchoolIdS,SubjectIdS,TeachingAreaIdS,limit);
                return list;
            }
        } else {
            return null;
        }
    }

    @Override
    public Teacherinfo findTeacherDetail(@Param("userId") Integer userId) {
        Teacherinfo as = seekTeacherDao.findTeacherDetail(userId);
        if(as != null){
          List<TeacherUrlInfo> list1 = seekTeacherDao.findTeacherUrl1(userId);
          List<TeacherUrlInfo> list2 = seekTeacherDao.findTeacherUrl2(userId);
          as.setTeacherUrl1(list1);
          as.setTeacherUrl2(list2);
          List<SubjectInfo> list = seekTeacherDao.findUserSubject(userId);
          as.setSubject(list);
        }
        return as;
    }

    @Override
    @Transactional
    public int updateResume(ResumeInfo resumeInfo) {
        JSONArray subjectId = JSONArray.fromObject(resumeInfo.getSubejctId());
        int count = seekTeacherDao.findTeacherCount(resumeInfo.getUserId());
        if(count == 0){
            count = seekTeacherDao.insertTeacherResume(resumeInfo);
            if(count > 0){
                count = seekTeacherDao.insertUserSubject(resumeInfo.getUserId(),subjectId);
                if(count > 0){
                    int counts = seekTeacherDao.findGradeCount(resumeInfo.getUserId());
                    if(counts == 0){
                        count = seekTeacherDao.insertGrade(resumeInfo.getUserId());
                        if(count > 0){
                            return count;
                        }else {
                            throw new InsertException("新增等级失败");
                        }
                    }else{
                        return count;
                    }

                }else{
                    throw new InsertException("新增失败");
                }
            }else{
                throw new InsertException("新增失败");
            }
        }else{
            count = seekTeacherDao.updateResume(resumeInfo);
            if(count > 0){
                count = seekTeacherDao.deleteUserSubject(resumeInfo.getUserId());
                if(count > 0){
                    count = seekTeacherDao.insertUserSubject(resumeInfo.getUserId(),subjectId);
                    if(count > 0){
                        return count;
                    }else{
                        throw new InsertException("新增失败");
                    }
                }else{
                    throw new DeleteException("删除失败");
                }
            }else{
                throw new UpdateException("修改失败");
            }
        }
    }

    @Override
    public Teacherinfo findValueResume(Integer userId) {
        return seekTeacherDao.findValueResume(userId);
    }

    @Override
    public int findIsOne(Integer userId) {
        return seekTeacherDao.findIsOne(userId);
    }

    @Override
    public int insertGrade() {
        List<LoginInfo> userTeacher = seekTeacherDao.findUserTeacher();
        for (int i = 0; i <userTeacher.size(); i++){
            int id = userTeacher.get(i).getId();
            int count = seekTeacherDao.insertGrade(id);
            if(count < 1){
                return 0;
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int updateGrade(Integer userId, Integer gradeType) {
        int count = seekTeacherDao.updateGrade(userId,gradeType);
        if(count > 0){
            BigDecimal a = new BigDecimal(0);
            if(gradeType == 1){
                a = new BigDecimal(50);
            }else if(gradeType == 2){
                a = new BigDecimal(60);
            }else if(gradeType == 3){
                a = new BigDecimal(70);
            }else if(gradeType == 4){
                a = new BigDecimal(80);
            }else if(gradeType == 5){
                a = new BigDecimal(100);
            }
            count = seekTeacherDao.updateHourPrice(userId,a);
            if(count > 0){
                return count;
            }else{
                throw new UpdateException("修改失败");
            }
        }else{
            throw new UpdateException("修改失败");
        }
    }

    @Override
    public int updateAge(Integer userId, Integer age) {
        return seekTeacherDao.updateAge(userId,age);
    }

    @Override
    public PagingData findPosition(Integer page, Integer length) {
        List<LoginInfo> list = seekTeacherDao.findPosition(page,length);
        int count = seekTeacherDao.findPositionCount();
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }

    @Override
    public List<ItemBankInfo> findItemBankList(Integer limit) {
        return seekTeacherDao.findItemBankList(limit);
    }

    @Override
    public int deleteMyItemBank(Integer itemBankId) {
        return seekTeacherDao.deleteMyItemBank(itemBankId);
    }

    @Override
    public int deleteMyFeedBack(Integer feedBackId) {
        return seekTeacherDao.deleteMyFeedBack(feedBackId);
    }

    @Override
    public ItemBankInfo findItemBankDetail(Integer itemBankId) {
        ItemBankInfo info = seekTeacherDao.findItemBankDetail(itemBankId);
        List<ItemBankInfo> list = seekTeacherDao.findAnswerList(itemBankId);
        info.setAnswer(list);
        return info;
    }

    @Override
    public List<FeedBackInfo> findFeedBackList(Integer limit,Integer userId) {
        return seekTeacherDao.findFeedBackList(limit,userId);
    }

    @Override
    public FeedBackInfo findTeacherFeedBackDownStudentList1(Integer teacherFeedBackId) {
        FeedBackInfo info = seekTeacherDao.findFeedBackDetail(teacherFeedBackId);
        List<FeedBackInfo> list1 = seekTeacherDao.findTeacherFeedBackDownStudentList(teacherFeedBackId);
        info.setStudentList(list1);
        return info;
    }

    @Override
    public List<FeedBackInfo> findTeacherFeedBackDownStudentList(Integer limit, Integer userId) {
        List<FeedBackInfo> list = seekTeacherDao.findTeacherFeedBackList(limit,userId);
        for(FeedBackInfo info : list){
           List<FeedBackInfo> list1 = seekTeacherDao.findTeacherFeedBackDownStudentList(info.getId());
//           for(FeedBackInfo info1 : list1){
//                if(info1.getType() == 1){  //家长
//                    String a = seekTeacherDao.findMsg1(info.getUserId());
//                    info1.setStudentName(a);
//                }else{   //老师
//                    String a = seekTeacherDao.findMsg2(info.getUserId());
//                    info1.setTeacherName(a);
//                }
//           }
           info.setStudentList(list1);
        }
        return list;
    }

    @Override
    public int insertItemBank(String userId, String url,String content) {
        return seekTeacherDao.insertItemBank(userId,url,content);
    }

    @Override
    @Transactional
    public int insertAnswer(String userId, String itemBankId,String content, String url) {
        int count = seekTeacherDao.insertAnswer(userId,itemBankId,content,url);
        if(count > 0){
            String a = seekTeacherDao.findItemBankStatus(itemBankId);
            if(a.equals("YES")){
                return count;
            }else{
                count = seekTeacherDao.updateItemBankStatus(itemBankId);
                if(count > 0){
                    return count;
                }else{
                    throw new InsertException("修改失败");
                }
            }
        }else{
            throw new InsertException("新增失败");
        }
    }

    @Override
    public int insertFeedBack(String teaUserId, String stdUserId, String content, String url) {
        return seekTeacherDao.insertFeedBack(teaUserId,stdUserId,content,url);
    }

    @Override
    public int insertStdFeedBack(String userId, String teacherFeedBackId, String content, String url,int a) {
        return seekTeacherDao.insertStdFeedBack(userId,teacherFeedBackId,content,url,a);
    }

    @Override
    public int insertTeacherUrl(String teacherId, String url) {
        return seekTeacherDao.insertTeacherUrl(teacherId,url);
    }

    @Override
    public int insertTeacherUrl2(String teacherId, String url) {
        return seekTeacherDao.insertTeacherUrl2(teacherId,url);
    }

    @Override
    public List<StudentInfo> findTeacherUser(Integer limit, Integer userId) {
        List<StudentInfo> teacherUser = seekTeacherDao.findTeacherUser(limit, userId);
        for (int i = 0; i< teacherUser.size(); i++){
            StudentInfo info = teacherUser.get(i);
            List<SubjectInfo> list = seekTeacherDao.findUserSubject(info.getId());
            info.setSubjectInfo(list);
        }
        return teacherUser;
    }
}
